package com.kooison.trafficMonitor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.kooison.trafficMonitor.crawler.Crawler;
import com.kooison.trafficMonitor.crawler.ICrawler;

public class MonitorController extends AbstractController {

	private static Crawler crawler;
	
	public Crawler getCrawler() {
		return crawler;
	}

	public void setCrawler(Crawler crawler) {
		this.crawler = crawler;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		if (crawler == null) {
			crawler = new Crawler();
		}
		String time = crawler.getTime();
		Map locationsMap = crawler.getMap();
		
		Map map = new HashMap();
		map.put("time", time);
		map.put("locations", locationsMap);
		
		String view = ServletRequestUtils.getStringParameter(request, "view", "defaultVal");
		if (view.equals("map")) {
			return new ModelAndView("showTrafficInMap", map);
		} else if (view.equals("debug")) {
			return new ModelAndView("debug", map);
		} else {
			return new ModelAndView("showTraffic", map);
		}
	}
}
